package com.example.provider.javastudy.serializable;

import java.io.*;

/**
 * All rights Reserved, Designed By www.maihaoche.com
 *
 * @Package com.example.demo.serializable
 * @author: 红雪（hongxue@maihaoche.com）
 * @date: 2019/1/18 6:03 PM
 * @Copyright: 2017-2020 www.maihaoche.com Inc. All rights reserved.
 * 注意：本内容仅限于卖好车内部传阅，禁止外泄以及用于其他的商业项目
 */
public class TestSerializable {
    static int a = 1;

    public static void main(String[] args) {
        serializableToFile();
        Person person = fileToSerializable();

    }

    public static void serializableToFile(){

        Person person = new Person();
        person.setAge(11);
        person.setName("杜瑞");
        person.setSex("男");

        try {
            FileOutputStream outputStream = new FileOutputStream("/Users/durui/person.txt");
            ObjectOutputStream objectOutputStream = new ObjectOutputStream(outputStream);
            objectOutputStream.writeObject(person);
            objectOutputStream.close();
            outputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {

        }
    }

    public static Person fileToSerializable(){
        try {
            ObjectInputStream inputStream = new ObjectInputStream(new FileInputStream("/Users/durui/person.txt"));
            Person person = (Person)inputStream.readObject();

            File file = new File("/Users/durui/person.txt");
            FileInputStream fileInputStream = new FileInputStream("/Users/durui/person.txt");
            byte[] bytes = new byte[(int)file.length()];
            fileInputStream.read(bytes);

            for (byte aByte : bytes) {
                System.out.print(aByte+" ");
            }


            return person;
        } catch (IOException | ClassNotFoundException e) {
            e.printStackTrace();
        }
        return null;
    }
}
